package utils.document;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class ExcelUtil {

    static public void generatorRowFromList(Sheet sheet, List<Map<String, Object>> list){
        int rownum = 0;
        int colnum = 0;
        Row row;

        if(list == null || list.size() == 0){return;}

        //查将第一个元素作为表头
        Set<String> title = list.get(0).keySet();
        row = sheet.createRow(rownum);
        colnum = 0;
        for(String col: title){
            row.createCell(colnum).setCellValue(col);
        }

        //生成内容
        for(Map<String, Object> each: list){
            rownum++;
            row = sheet.createRow(rownum);
            colnum = 0;
            for(String col: title){
                Object obj = each.get(col);
                if(obj != null){
                    row.createCell(colnum).setCellValue(obj.toString());
                };
                colnum++;

            }
        }
    }

}
